import sys, inspect, os
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtNetwork import *

app = qApp

def processTheDatagram(datagram):
    print 'Hello Data\n'

class UdpServer(QObject):

    def __init__(self, parent=None, port=8877):
        super(UdpServer, self).__init__(parent=parent)
        self.port = port
        self.udpSocket = None

    def setPort(self, port=8877):
        self.port = port

    def initSocket(self):
        self.udpSocket = QUdpSocket(self)
        self.udpSocket.bind(QHostAddress.Any, self.port)

        self.udpSocket.readyRead.connect(self.readPendingDatagrams)

    def readPendingDatagrams(self):
        while self.udpSocket.hasPendingDatagrams():
            datagram = QByteArray()
            datagram.resize(self.udpSocket.pendingDatagramSide())

            (senderIp, senderPort) = self.udpSocket.readDatagram(datagram.data(), datagram.size())

            processTheDatagram(datagram)

            
if  __name__ == "__main_":
    server = UdpServer()
    server.initSocket()

